<?php
require_once ("secure_area.php");
require_once ("interfaces/idata_controller.php");
class SSF_configs extends Secure_area implements iData_controller 
{
	function __construct()
	{
		parent::__construct('ssf_configs');
	}
	
	function get_sortable_data()
	{
		$sortable_data = array(); 
		$all_ssf = $this->SSF_config->get_all();
		foreach($all_ssf->result() as $row)
		{
			$raw_data = array();
			$ssf_id = $row->ssf_id;
			$name = $row->name;
			//Get ssf Components
			$ssf_comp_result = $this->SSF_config->get_ssf_coponents($ssf_id);
			$ssf_comp_string =  '';
			foreach($ssf_comp_result->result() as $row)
			{
				$ssf_comp_string .= 'Tier' .$row->order.' @ '.$row->percent_value.'%'.'&nbsp;&nbsp;&nbsp;';
			}
			
			if(empty($ssf_comp_string))
			{
				$ssf_comp_string = 'None';
			}
			
			//Get ssf Components for Presentation
			$ssf_present_result = $this->SSF_config->get_ssf_presentation($ssf_id);
			$ssf_present_string =  '';
			foreach($ssf_present_result->result() as $row)
			{
				$ssf_present_string .= 'Tier' .$row->order.' @ '.$row->percent_value.'%'.'&nbsp;&nbsp;&nbsp;';
			}
			
			if(empty($ssf_present_string))
			{
				$ssf_present_string = 'None';
			}
			
			
			$raw_data[] = $ssf_id;
			$raw_data[] = $name;
			$raw_data[] = $ssf_comp_string;
			$raw_data[] = $ssf_present_string;
			
			$sortable_data[] = $raw_data;	
		}	
		return $sortable_data;
	}
	
	
	function index()
	{
		$sortable_data = array(); 
		$sortable_data = $this->get_sortable_data();
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();		
		$data['manage_table']=get_ssf_manage_table($sortable_data,$this);
		$this->load->view('ssf/manage',$data);
	}
	
	function search()
	{
		$sortable_data = array(); 
		$search=$this->input->post('search');
		$sortable_data = $this->get_sortable_data();
		$data_rows=get_ssf_manage_table_data_rows($sortable_data,$this);
		echo $data_rows;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->SSF_config->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	function get_row()
	{
		$sortable_data = array(); 
		$item_id = $this->input->post('row_id');
		$all_ssf = $this->SSF_config->get_info($item_id);
		foreach($all_ssf->result() as $row)
		{
			$raw_data = array();
			$ssf_id = $row->ssf_id;
			$name = $row->name;
			//Get ssf Components
			$ssf_comp_result = $this->SSF_config->get_ssf_coponents($ssf_id);
			$ssf_comp_string =  '';
			foreach($ssf_comp_result->result() as $row)
			{
				$ssf_comp_string .= $row->amount.' @ '.$row->percent_value.'%'.'&nbsp;&nbsp;&nbsp;';
			}
			if(empty($ssf_comp_string))
			{
				$ssf_comp_string = 'None';
			}
			
			$raw_data[] = $ssf_id;
			$raw_data[] = $name;
			$raw_data[] = $ssf_comp_string;
			
			$sortable_data[] = $raw_data;	
		}	
		$data_row=get_ssf_data_row($sortable_data,$this);
		echo $data_row;
	}
	
	function view($item_id=-1)
	{
		$data['ssf_info']=$this->SSF_config->get_info($item_id);
		$data['ssf_componets']=$this->SSF_config->get_ssf_coponents($item_id);
		//$data['default_ssf_2_rate']=($item_id==-1) ? $this->Appconfig->get('default_ssf_2_rate') : '';
		$this->load->view("ssf/form",$data);
	}
	
	function edit_ssf($ssf_id=-1)
	{				
		$ssf_info = $this->SSF_config->get_ssf_for_editing($ssf_id);
		$ssf = $ssf_info['query_ssf']->row();
		$ssf_items = edit_ssf_items_manage_table($ssf_info['query_ssf_items'],$this,$ssf->start);
		$data['manage']=$ssf_items;
		$data['ssf']=$ssf;
		$data['controller_name']=strtolower($this->uri->segment(1));
				
		$this->load->view("ssf/edit_ssf", $data);
	}
	
	function edit_ssf_present($ssf_id=-1)
	{				
		$ssf_info = $this->SSF_config->get_ssf_for_editing_present($ssf_id);
		$ssf = $ssf_info['query_ssf']->row();
		$ssf_items = edit_ssf_items_manage_table($ssf_info['query_ssf_items'],$this,$ssf->start);
		$data['manage']=$ssf_items;
		$data['ssf']=$ssf;
		$data['controller_name']=strtolower($this->uri->segment(1));
				
		$this->load->view("ssf/edit_ssf_presentation", $data);
	}
	
	function save($item_id=-1)
	{
		$item_data = array(
		'name'=>$this->input->post('name'),
		'start'=>$this->input->post('order_radio')
		);
		///////////////////////////////////////////////////////////////////////////////////////////
		
		//Order items
		$order = array();
		$percent_value = array();
		$final_pair_array = array();
			
		foreach($this->input->post('order') as $datax)
		{
			$order[] = $datax;
		}
		
		foreach($this->input->post('percent_value') as $datax)
		{
			$percent_value[] = $datax;
		}
			
		//Put them in One array as key and value
		for($x=0; $x < count($order); $x++)
		{
			$final_array[$order[$x]] = $percent_value[$x];	
		}
			
		//save the order
		$return_id = $this->SSF_config->save($item_data,$item_id);
		if($return_id)
		{	
			$ssf_id = $item_data['ssf_id'];
			$number_of_processed = 0;
			foreach($final_array as $key => $value)
			{
			  $percent_value = $value;
			  
			  $compo_data = array(
			  'ssf_id'=>$ssf_id,
			  'order'=>$key,
			  'percent_value'=>$percent_value
			  );
			  
			  $db_result = $this->SSF_config->save_ssf_items($compo_data);
			  $this->SSF_config->save_ssf_items_presentation($compo_data);
			  if($db_result)
			  {
				  $number_of_processed++;
			  }
			}
				
			if($number_of_processed == count($final_array))
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_adding').' '.
				$item_data['name'],'item_id'=>$ssf_id));
			}
			else
			{
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_error_adding_updating').' '.
			$item_data['name'],'item_id'=>-1));
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////
	}
	
	function update_ssf()
	{			
		$item_data = array(
		'name'=>$this->input->post('name'),
		'start'=>$this->input->post('order_radio')
		);
		
		$ssf_id = $this->input->post('hidden_ssf');
		$hidden_empty = $this->input->post('hidden_empty');
		
		if($hidden_empty == 1)
		{
			//Order items
			$order = array();
			$percent_value = array();
			$item_update_id = array();
			$final_pair_array = array();

			foreach($this->input->post('order') as $datax)
			{
				$order[] = $datax;
			}
		
			foreach($this->input->post('percent_value') as $datax)
			{
				$percent_value[] = $datax;
			}
		
			foreach($this->input->post('item_update_id') as $datax)
			{
				$item_update_id[] = $datax;
			}
		
			//Merge the amount and percent_value
			for($x=0; $x < count($percent_value); $x++)
			{
				$pair_array = array();
				$pair_array[0] = $item_update_id[$x];
				$pair_array[1] = $percent_value[$x];
			
				$final_pair_array[] = $pair_array;	
			}
			
			//Put them in One array as key and value
			for($x=0; $x < count($order); $x++)
			{
				$final_array[$order[$x]] = $final_pair_array[$x];	
			}
		}
		
		//save the order
		if($hidden_empty == 1)
		{
			if($this->SSF_config->update_ssf($item_data,$ssf_id))
			{		
				$number_of_processed = 0;
				foreach($final_array as $key => $value)
				{
					$update_id = $value[0];//cost
					$percent_value = $value[1];//quantity
					
					$compo_data = array(
					'ssf_id'=>$ssf_id,
					'order'=>$key,
					'percent_value'=>$percent_value
					);
				
					$db_result = $this->SSF_config->update_ssf_items($compo_data,$update_id);
					if($db_result)
					{
						$number_of_processed++;
					}
				}
				
				if($number_of_processed == count($final_array))
				{
					echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_adding').' '.
					$item_data['name'],'item_id'=>$ssf_id));
				}
				else
				{
					echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
					$item_data['name'],'item_id'=>-1));
				}
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		else
		{
			if($this->SSF_config->update_ssf($item_data,$ssf_id))
			{		
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_adding').' '.
				$item_data['name'],'item_id'=>$ssf_id));
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}		
		
	}
	
	
	function update_ssf_presentation()
	{			
		$item_data = array(
		'name'=>$this->input->post('name'),
		'start'=>$this->input->post('order_radio')
		);
		
		$ssf_id = $this->input->post('hidden_ssf');
		$hidden_empty = $this->input->post('hidden_empty');
		
		if($hidden_empty == 1)
		{
			//Order items
			$order = array();
			$percent_value = array();
			$item_update_id = array();
			$final_pair_array = array();

			foreach($this->input->post('order') as $datax)
			{
				$order[] = $datax;
			}
		
			foreach($this->input->post('percent_value') as $datax)
			{
				$percent_value[] = $datax;
			}
		
			foreach($this->input->post('item_update_id') as $datax)
			{
				$item_update_id[] = $datax;
			}
		
			//Merge the amount and percent_value
			for($x=0; $x < count($percent_value); $x++)
			{
				$pair_array = array();
				$pair_array[0] = $item_update_id[$x];
				$pair_array[1] = $percent_value[$x];
			
				$final_pair_array[] = $pair_array;	
			}
			
			//Put them in One array as key and value
			for($x=0; $x < count($order); $x++)
			{
				$final_array[$order[$x]] = $final_pair_array[$x];	
			}
		}
		
		//save the order
		if($hidden_empty == 1)
		{
			if($this->SSF_config->update_ssf($item_data,$ssf_id))
			{		
				$number_of_processed = 0;
				foreach($final_array as $key => $value)
				{
					$update_id = $value[0];//cost
					$percent_value = $value[1];//quantity
					
					$compo_data = array(
					'ssf_id'=>$ssf_id,
					'order'=>$key,
					'percent_value'=>$percent_value
					);
				
					$db_result = $this->SSF_config->update_ssf_items_presentation($compo_data,$update_id);
					if($db_result)
					{
						$number_of_processed++;
					}
				}
				
				if($number_of_processed == count($final_array))
				{
					echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_adding').' '.
					$item_data['name'],'item_id'=>$ssf_id));
				}
				else
				{
					echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
					$item_data['name'],'item_id'=>-1));
				}
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}
		else
		{
			if($this->SSF_config->update_ssf($item_data,$ssf_id))
			{		
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_adding').' '.
				$item_data['name'],'item_id'=>$ssf_id));
			}
			else//failure
			{	
				echo json_encode(array('success'=>false,'message'=>$this->lang->line('ssf_error_adding_updating').' '.
				$item_data['name'],'item_id'=>-1));
			}
		}		
		
	}
	
	
	function delete()
	{
		$items_to_delete=$this->input->post('ids');
		
		if($this->SSF_config->delete_list($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_deleted').' '.
			count($items_to_delete).' '.$this->lang->line('items_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	function delete_ssf_item()
	{
		$items_to_delete = $this->input->post('row_id');
		$ssf_id = $this->input->post('ssf_id');
		
		if($this->SSF_config->delete_ssf_item($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_deleted').' '.
			'','item_id'=>$ssf_id));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	function delete_ssf_item_presentation()
	{
		$items_to_delete = $this->input->post('row_id');
		$ssf_id = $this->input->post('ssf_id');
		
		if($this->SSF_config->delete_ssf_item_presentation($items_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('ssf_successful_deleted').' '.
			'','item_id'=>$ssf_id));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 500;
	}
	
////////////  MEK /////////////////////////////////////////////////////////////////////////////////
	

	
	
	
	
}
?>